package com.ruoyi.project.websiteContentTitle.service.impl;

import java.util.List;
import java.util.ArrayList;
import com.ruoyi.framework.web.domain.Ztree;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.websiteContent.mapper.WebsiteContentMapper;
import com.ruoyi.project.websiteContentTitle.domain.WebsiteTitle;
import com.ruoyi.project.websiteContentTitle.mapper.WebsiteTitleMapper;
import com.ruoyi.project.websiteContentTitle.service.IWebsiteTitleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.utils.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 网站首页展示目录Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-08-03
 */
@Service("title")
public class WebsiteTitleServiceImpl implements IWebsiteTitleService
{
    @Autowired
    private WebsiteTitleMapper websiteTitleMapper;

    @Autowired
    private WebsiteContentMapper websiteContentMapper;

    /**
     * 查询网站首页展示目录
     * 
     * @param titleId 网站首页展示目录ID
     * @return 网站首页展示目录
     */
    @Override
    public WebsiteTitle selectWebsiteTitleById(Long titleId)
    {
        return websiteTitleMapper.selectWebsiteTitleById(titleId);
    }

    /**
     * 查询网站首页展示目录列表
     * 
     * @param websiteTitle 网站首页展示目录
     * @return 网站首页展示目录
     */
    @Override
    public List<WebsiteTitle> selectWebsiteTitleList(WebsiteTitle websiteTitle)
    {
        return websiteTitleMapper.selectWebsiteTitleList(websiteTitle);
    }

    /**
     * 新增网站首页展示目录
     * 
     * @param websiteTitle 网站首页展示目录
     * @return 结果
     */
    @Override
    public int insertWebsiteTitle(WebsiteTitle websiteTitle)
    {
        websiteTitle.setCreateTime(DateUtils.getNowDate());
        return websiteTitleMapper.insertWebsiteTitle(websiteTitle);
    }

    /**
     * 修改网站首页展示目录
     * 
     * @param websiteTitle 网站首页展示目录
     * @return 结果
     */
    @Override
    public int updateWebsiteTitle(WebsiteTitle websiteTitle)
    {
        websiteTitle.setUpdateTime(DateUtils.getNowDate());
        return websiteTitleMapper.updateWebsiteTitle(websiteTitle);
    }

    /**
     * 删除网站首页展示目录对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteWebsiteTitleByIds(String ids)
    {
        return websiteTitleMapper.deleteWebsiteTitleByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除网站首页展示目录信息
     * 
     * @param titleId 网站首页展示目录ID
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteWebsiteTitleById(Long titleId)
    {
        websiteContentMapper.deleteContentByTitleId(titleId);
        return websiteTitleMapper.deleteWebsiteTitleById(titleId);
    }

    /**
     * 查询网站首页展示目录树列表
     * 
     * @return 所有网站首页展示目录信息
     */
    @Override
    public List<Ztree> selectWebsiteTitleTree()
    {
        List<WebsiteTitle> websiteTitleList = websiteTitleMapper.selectWebsiteTitleList(new WebsiteTitle());
        List<Ztree> ztrees = new ArrayList<Ztree>();
        for (WebsiteTitle websiteTitle : websiteTitleList)
        {
            Ztree ztree = new Ztree();
            ztree.setId(websiteTitle.getTitleId());
            ztree.setpId(websiteTitle.getParentId());
            ztree.setName(websiteTitle.getTitleName());
            ztree.setTitle(websiteTitle.getTitleName());
            ztrees.add(ztree);
        }
        return ztrees;
    }

    /**
     * 获取所有一级目录
     * @return
     */
    @Override
    public List<WebsiteTitle> getAllOneTitle() {
        return websiteTitleMapper.getAllOneTitle();
    }

    /**
     * 根据一级目录获取二级目录标题
     * @param titleId
     * @return
     */
    @Override
    public List<WebsiteTitle> getAllShowTwoTitle(long titleId) {
        return websiteTitleMapper.getAllShowTwoTitle(titleId);
    }
}
